#include "lexercc.hpp"

using namespace std;

void cmd(int argc, char *args[]);

int main(int argc, char *args[]) {
    // Proccess command line
    cmd(argc, args);
    fstream fin;
    __OPEN_AND_CHECK(fin, args[1], false);

    // Use factory method to build lexer from JSON file
    MyLexer lexer = buildLexerFromJson(fin);
    writeLexer(lexer, args[2]);

    /****** A simple demo, uncomment it and have fun :) ******/
    string s;
    while (true) {
        cout << "Input the string to identify: ";
        getline(cin, s);
        vector<Token> tokens;
        try{
            tokens = lexer.lex(s);
        }catch(ParseException e){
            continue;
        }
        for (auto i : tokens) {
            cout << i.tokenName << ": " << i.word << endl;
        }
    }
    return 0;
}


void cmd(int argc, char *args[]){
    string header = "* Zeege (R) Lexical Analyzer Generator Version 1.0.0\n"
                    "* Copyright(C) Zeege. All rights reserved.\n";
    string asteroids =
        "\n*************************************************************\n";
    cout << asteroids << header << "* Welcome to MyLex! Happy lexing! >_<" << asteroids
         << "\n\n";
    string promptStr = "Invalid arguments.\nUsage: main.exe {lex definition filepath} {lexer output dir}\n"
                        "e.g. main.exe abc.json ./abclexer\n";
    if (argc != 3) {
        cout << promptStr << endl;
        exit(0);
    }
}
